Uec3.ReceiptAddressWindow = Ext.extend(Ext.app.Module, {
	id : 'receiptAddress',
	type : 'common/receiptAddress',

	/**
	 * Initialize this module. This function is called at startup (page
	 * load/refresh).
	 */
	init : function(initParams) {
	},

	paramsCollection : new Ext.util.MixedCollection(true),

	getParamsByKey : function(key) {
		return this.paramsCollection.get(key);
	},
	browseReceiptAddress : function(id) {

		var receiptAddress_ds = this.getParamsByKey('receiptAddress_ds');

		var receiptAddress_form = this.getParamsByKey('receiptAddress_form');

		var receiptAddress_win = this.getParamsByKey('receiptAddress_win');

		var record = receiptAddress_ds.getById(id);

		receiptAddress_form.getForm().loadRecord(record);

		receiptAddress_win.buttons[0].hide();

		receiptAddress_win.buttons[1].hide();

		receiptAddress_win.show();

		receiptAddress_win.setTitle('浏览收货地址-按Esc退出');

		var maskTopField = this.getParamsByKey('maskTopField');
		
		maskTopField.show();

	},

	editReceiptAddress : function(id) {

		var receiptAddress_ds = this.getParamsByKey('receiptAddress_ds');

		var receiptAddress_form = this.getParamsByKey('receiptAddress_form');

		var receiptAddress_win = this.getParamsByKey('receiptAddress_win');

		var receiptAddress_city_cmb = this
				.getParamsByKey('receiptAddress_city_cmb');

		var receiptAddress_dist_cmb = this
				.getParamsByKey('receiptAddress_dist_cmb');

		var record = receiptAddress_ds.getById(id);

		receiptAddress_form.getForm().loadRecord(record);

		var province = record.get('province');

		var city = record.get('city');

		var provinceId = record.get('provinceId');

		var cityId = record.get('cityId');

		if (province != "") {

			receiptAddress_city_cmb.getStore().setBaseParam("prov", provinceId);
			receiptAddress_city_cmb.getStore().load();

		}

		if (city != "") {

			receiptAddress_dist_cmb.getStore().setBaseParam("city", cityId);
			receiptAddress_dist_cmb.getStore().load();

		}

		receiptAddress_win.buttons[0].show();
		receiptAddress_win.buttons[1].show();

		receiptAddress_win.show();

		receiptAddress_win.buttons[0].setText('更新');

		receiptAddress_win.setTitle('编辑收货地址-按Esc退出');

	},

	deleteReceiptAddress : function(id) {

		var receiptAddress_ds = this.getParamsByKey('receiptAddress_ds');
		var receiptAddress_grid = this.getParamsByKey('receiptAddress_grid');

		var record = receiptAddress_ds.getById(id);

		Ext.Msg.confirm('信息', '确定要删除?', function(btn) {
			if (btn == 'yes') {
				receiptAddress_grid.loadMask.show();
				var receiptAddressId = record.get('id');
				Ext.Ajax.request({
							url : Uec3.App.Constants.homeUrl
									+ '/receiptAddress/edit_receiptAddress!delete.do',
							success : function(res) {
								var result = res.responseText;
								if (!result) {
									receiptAddress_grid.loadMask.hide();
									receiptAddress_ds.remove(record);
									receiptAddress_grid.getView().refresh();
									Ext.Msg.alert("提示","删除成功");

								} else {
									receiptAddress_grid.loadMask.hide();
									Ext.Msg.alert("提示", Ext
													.decode(result.failture));
								}

							},

							failure : function(res) {
								receiptAddress_grid.loadMask.hide();
								Ext.Msg.alert('网络异常,请重先请求');

							},

							params : {
								id : receiptAddressId
							}

						});

			}
		});
	},

	getComponents : function(module) {

		var receiptAddress_dist_cmb;
		var receiptAddress_city_cmb;
		var receiptAddress_prov_cmb;
		var receiptAddress_ds;
		var receiptAddress_grid;
		var receiptAddress_win;
		var receiptAddress_form;
		var receipt_ajax;
        var keymap;
		/**Ext.data.Store.prototype.applySort = function() {

			if (this.sortInfo && !this.remoteSort) {

				var s = this.sortInfo, f = s.field;
				var st = this.fields.get(f).sortType;
				var fn = function(r1, r2) {

					var v1 = st(r1.data[f]), v2 = st(r2.data[f]);
					if (typeof(v1) == "string") {

						return v1.localeCompare(v2);
					}
					return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);

				}
				this.data.sort(s.direction, fn);

				if (this.snapshot && this.snapshot != this.data) {

					this.snapshot.sort(s, direction, fn);

				}

			}

		};*/

		getGridComboBox = function() {

			// 区县下拉框
			receiptAddress_dist_cmb = getDistrictCombox(Uec3.App.Constants.homeUrl);
			receiptAddress_dist_cmb.anchor="99%";	
			//receiptAddress_dist_cmb.allowBlank=false;
			receiptAddress_dist_cmb.blankText="收获地址地区必须填写";
		    receiptAddress_dist_cmb.setWidth(162);
			// 城市下拉框
			receiptAddress_city_cmb = getCityCombox(Uec3.App.Constants.homeUrl,
					receiptAddress_dist_cmb);
					receiptAddress_city_cmb.anchor="99%";
					receiptAddress_city_cmb.blankText="收获地址城市必须填写";
					receiptAddress_city_cmb.allowBlank=false;
			receiptAddress_city_cmb.setWidth(162);
			// 省份下拉框
			receiptAddress_prov_cmb = getProvinceCombox(
					Uec3.App.Constants.homeUrl, receiptAddress_city_cmb,
					receiptAddress_dist_cmb);
					receiptAddress_prov_cmb.allowBlank=false;
					receiptAddress_prov_cmb.blankText="收获地址省份必须填写";
					receiptAddress_prov_cmb.anchor="99%";
			receiptAddress_prov_cmb.setWidth(162);

		};

		function renderDesc(value, cellmeta, record, rowIndex, columnIndex,
				store) {
			
			var str;
			if(module.app.hasPermission(19)){
			 tr = "<a style='font-size:12px' href='javascript:Ext.CUR_APP.getModule(\"receiptAddress\").browseReceiptAddress("
					+ rowIndex
					+ ");'>查看</a>|"
					+ "<a style='font-size:12px;margin-left:12px'   href='javascript:Ext.CUR_APP.getModule(\"receiptAddress\").editReceiptAddress("
					+ rowIndex
					+ ");'>编辑</a>|"
					+ "<a style='font-size:12px;margin-left:12px' href='javascript:Ext.CUR_APP.getModule(\"receiptAddress\").deleteReceiptAddress("
					+ rowIndex + ");'>删除</a>|"
			}else{
				
		     str = "<a style='font-size:12px' href='javascript:Ext.CUR_APP.getModule(\"receiptAddress\").browseReceiptAddress("
					+ rowIndex
					+ ");'>查看</a>|"
				
			}
			return str;

		};

		var getAndReceiptAddressRestore = function() {

			receiptAddress_ds = new Ext.data.Store({
				// proxy: new
				// Ext.data.MemoryProxy(myData),
				proxy : new Ext.data.HttpProxy({
							url : Uec3.App.Constants.homeUrl
									+ '/receiptAddress/edit_receiptAddress!query.do'
						}),
				baseParams : {
					start : 0,
					limit : 20
				},
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'root'
						}, [

								{
									name : 'id'
								}, {
									name : 'companyId'
								}, {

									name : 'areaId'
								}, {
									name : 'province'
								}, {
									name : 'city'
								}, {
									name : 'district'
								}, {
									name : 'provinceId'
								}, {
									name : 'cityId'
								}, {
									name : 'street'
								}, {
									name : 'postCode'
								}, {
									name : 'receiverName'
								}, {
									name : 'phone'
								}, {
									name : 'mobile'
								}

						]),
				remoteSort : true,

				// 默认排序字段
				sortInfo : {
					field : "id",
					direction : "asc"
				}

			});

		};

		var receiptAddresscomponentInits = function() {

			// 得到下拉菜单列表
			getGridComboBox();

			// 初始化receiptAddress表格对象

			var receiptAddress_colModel = new Ext.grid.ColumnModel([
					new Ext.grid.RowNumberer(),

					{
						header : "操作",
						width : 120,
						dataIndex : 'id',
						renderer : module.moduleActionRenderer([{
							func:'browseReceiptAddress',
							desc:'查看',
							permission:18
						},{
							func:'editReceiptAddress',
							desc:'编辑',
							permission:19
						},
						{
							func:'deleteReceiptAddress',
							desc:'删除',
							permission:19
						}])
					}, {
						header : "省",
						width : 80,
						dataIndex : 'province'
					}, {
						header : "市",
						width : 80,
						dataIndex : 'city'

					}, {
						header : "区",
						width : 80,
						dataIndex : 'district'

					}, {
						header : "邮编",
						width : 80,
						dataIndex : 'postCode'
					}, {
						header : "联系人",
						width : 80,
						dataIndex : 'receiverName'
					}, {
						header : "电话",
						width : 80,
						dataIndex : 'phone'
					}, {
						header : "手机",
						width : 100,
						dataIndex : 'mobile'
					}, {
						header : "地址",
						width : 160,
						dataIndex : 'street'
					}]);

			// create the Grid
			receiptAddress_grid = new Ext.grid.GridPanel(

			{
						// title:'收货地址列表',
						region : 'center',
						iconCls : 'icon-grid',
						enableColumnMove : false,
						enableColumnMoveResize : false,
						stripeRows : true,
						height : 500,
						loadMask : {
							msg : "...加载数据中"
						},
						store : receiptAddress_ds,
						cm : receiptAddress_colModel,
						sm : new Ext.grid.RowSelectionModel({
									singleSelect : true
								}),
						viewConfig : {
							forceFit : true,
							columnsText : '显示的列',
							scrollOffset : 20,
							sortAscText : '升序',
							sortDescText : '降序'
						},
						tbar : new Ext.app.ModuleToolbar(['-', {
							text : '增加',
							permission:19,
							icon : Uec3.App.Constants.deskUrl
									+ "/resources/images/default/dd/drop-add.gif",
							handler : function() {
							
							

								if (!(receiptAddress_form.getForm().getEl())) {
									receiptAddress_form.getForm().reset();
								} else {
									receiptAddress_form.getForm().getEl().dom
											.reset();
								}
								receiptAddress_form.getForm().findField('id')
										.setValue('');
								receiptAddress_form.getForm()
										.findField('areaId').setValue('');
								receiptAddress_win.buttons[0].show();
								receiptAddress_win.buttons[1].show();
								receiptAddress_win.show();
								receiptAddress_win.buttons[0].setText('增加');
								receiptAddress_win.setTitle('增加收货地址-按Esc退出');
								receiptAddress_form.setDisabled(false);
							}
						}]),
						bbar : new Ext.PagingToolbar({
									pageSize : 20,
									store : receiptAddress_ds,
									display : true,
									displayInfo : true,
									displayMsg : '第 {0} - {1} 条  共 {2} 条',
									emptyMsg : "没有记录",
									items : [/**
												 * '-', { pressed: true,
												 * enableToggle:true, text:
												 * 'Show Preview', cls:
												 * 'x-btn-text-icon details',
												 * toggleHandler: function(btn,
												 * pressed){ var view =
												 * receiptAddress_grid.getView();
												 * view.showPreview = pressed;
												 * view.refresh(); } }
												 */
									]
								})

					});

			//receiptAddress_ds.load();

			// 初始化表单对象
			receiptAddress_form = new Ext.form.FormPanel({
						labelAlign : 'right',
						labelWidth : 60,
						// title:客户档案,
						frame : true,
						width : 350,
						height : 90,
						url : '',
						defaultType : 'textfield',
						layout:'uec3grid',
						layoutConfig:{
//							hGrid:[8,25,8,25,8,25],
//							vGrid:[33,33,33]
							hGrid:[9,18,6,18,6,18,7,18],
							vGrid:[33,33,33]
							
						},
						items : [
						    {
								name : 'id',
								fieldLabel : 'ID',
								xtype : 'hidden'
							}, {
								name : 'companyId',
								fieldLabel : '公司ID',
								xtype : 'hidden'
							}, {
								name : 'areaId',
								fieldLabel : '地区ID',
								xtype : 'hidden'
							}, 
							{xtype:'label',text:'省'},
							receiptAddress_prov_cmb,
							{xtype:'label',text:'市'},
							receiptAddress_city_cmb,
							{xtype:'label',text:'区'},
							receiptAddress_dist_cmb,
							{xtype:'label',text:'邮编'},
							{
								name : 'postCode',
								width:210,
								xtype : 'textfield',
								regex : /^[0-9]+$/,
							    regexText : '请输入阿拉伯数字格式',
							    maxLength:"6",
								maxLengthText:"邮编最大长度为6个字符"
							},
							{xtype:'label',text:'通讯地址'},								
							{
								name : 'street',
								xtype : 'textfield',
								preventScrollbars : true,
								anchor:"99%",
							    maxLength:"60",
								maxLengthText:"最大长度为60位",
								grid:[2,2,8,2]
							},
							{xtype:'label',text:'联系人'},							
							{
								name : 'receiverName',
								width:210,
							    maxLength:"30",
								maxLengthText:"最大长度为30位"
							},
							{xtype:'label',text:'电话'},							
							{
								name : 'phone',
								width:210,
								xtype : 'textfield',
							    maxLength:"30",
								maxLengthText:"最大长度为30位"
							},
							{xtype:'label',text:'手机'},							
							{
								name : 'mobile',
								xtype : 'textfield',
								width:210,
								regex : /^[0-9]+$/,
							    regexText : '请输入阿拉伯数字格式',
							    maxLength:"30",
								maxLengthText:"最大长度为30位"
							}
						]
					});

			receipt_ajax = new Ext.data.Connection();
			// 初始化window对象
			receiptAddress_win = new Ext.Window({
				layout : 'fit',
				constraitHeader:true,
				closeAction : 'hide',
				width : 660,
				title : '增加收货地址-按Esc退出',
				collapsible : true,
				constrait : true,
				autoScroll : true,		
				modal : true,
				draggable : true,
				plain : true,
				filter : 'alpha(opacity:.2)',
				header : true,
				resizable : true,
				items : [receiptAddress_form],
				buttons : [{
					text : '保存',
					iconCls:"icon-save",
					handler : function() {
					

						if (!receiptAddress_form.getForm().isValid()) {

							Ext.Msg.alert("提示", "请按照要求进行正确操作");

							return;

						}
						var formValues = receiptAddress_form.getForm()
								.getValues();
						var jsonAll = Ext.encode(formValues);

						receipt_ajax.request({
							url : Uec3.App.Constants.homeUrl
									+ '/receiptAddress/edit_receiptAddress!save.do',
							success : function(res) {

								var result = Ext.decode(res.responseText);
								if (result.success == "OK") {
									receiptAddress_win.hide();

									receiptAddress_ds.reload();

								} else {

									Ext.Msg.alert("提示",
											result.failure);
											receiptAddress_ds.reload();

								}
							},

							failure : function(res) {

								Ext.Msg.alert("提示","网络出现异常,请重新连接" );

							},

							params : {
								jsonAll : jsonAll
							}

						});

					}

				}, {
					text : '关闭',
					iconCls:"icon-close",
					handler : function() {

						receiptAddress_win.hide();

					}

				}],

				buttonAlign : 'center'

			});
		};

		var module=this;
		var controlReceiptAddressEvent = function() {

			receiptAddress_prov_cmb.on('select', function(combo) {

						var value = combo.getValue();

						receiptAddress_form.getForm().findField("areaId")
								.setValue(value);

					});

			receiptAddress_city_cmb.on('select', function(combo) {

						var value = combo.getValue();

						receiptAddress_form.getForm().findField("areaId")
								.setValue(value);

					});

			receiptAddress_dist_cmb.on('select', function(combo) {

						var value = combo.getValue();

						receiptAddress_form.getForm().findField("areaId")
								.setValue(value);

					});

			receipt_ajax.on('beforerequest', function() {

						Ext.MessageBox.wait("保存数据中...", "提示");
					});

			receipt_ajax.on('requestcomplete', function() {

						Ext.MessageBox.hide();
					});
			
			receiptAddress_win.on("show",function(){
					
					keymap = new Ext.KeyMap(receiptAddress_win.id, {
				    key: 13, // or Ext.EventObject.ENTER
				    fn: function(){
			
							if (!receiptAddress_form.getForm().isValid()) {

							Ext.Msg.alert("提示", "请按照要求进行正确操作");

							return;

						}
						var formValues = receiptAddress_form.getForm()
								.getValues();
						var jsonAll = Ext.encode(formValues);

						receipt_ajax.request({
							url : Uec3.App.Constants.homeUrl
									+ '/receiptAddress/edit_receiptAddress!save.do',
							success : function(res) {

								var result = Ext.decode(res.responseText);
								if (result.success == "OK") {
									receiptAddress_win.hide();

									receiptAddress_ds.reload();

								} else {

									Ext.Msg.alert("提示",
											result.failure);
											receiptAddress_ds.reload();

								}
							},

							failure : function(res) {

								Ext.Msg.alert("提示","网络出现异常,请重新连接" );

							},

							params : {
								jsonAll : jsonAll
							}

						});
					
				},
				    scope: receiptAddress_win
				});
				
	    		var maskTopField = new Ext.LoadMaskJUPIT(receiptAddress_form.id);
	    		module.paramsCollection.add("maskTopField", maskTopField);
			},this,{singe:true});
			

			receiptAddress_win.on("hide",function(){
					
				keymap.setDisabled(true);
	    		var maskTopField = module.getParamsByKey("maskTopField");
	    		if(maskTopField)maskTopField.hide();
	    		
			},this);

			/**
			 * receiptAddress_ds.on('load',function(){
			 * 
			 * var me=this; var records=me.getRange();
			 * Ext.each(records,function(record){
			 * 
			 * var rowId=me.indexOf(record);
			 * 
			 * Ext.get('browseReceiptAddress_'+rowId).on('click',function(){
			 * 
			 * browseReceiptAddress(rowId);
			 * 
			 * });
			 * 
			 * Ext.get('editReceiptAddress_'+rowId).on('click',function(){
			 * 
			 * editReceiptAddress(rowId);
			 * 
			 * });
			 * 
			 * Ext.get('deleteReceiptAddress_'+rowId).on('click',function(){
			 * 
			 * deleteReceiptAddress(rowId);
			 * 
			 * }); })
			 * 
			 * });
			 */

		};

		getAndReceiptAddressRestore();

		receiptAddresscomponentInits();

		controlReceiptAddressEvent();

		this.paramsCollection.add("receiptAddress_ds", receiptAddress_ds);
		this.paramsCollection.add("receiptAddress_form", receiptAddress_form);
		this.paramsCollection.add("receiptAddress_win", receiptAddress_win);
		this.paramsCollection.add("receiptAddress_dist_cmb",
				receiptAddress_dist_cmb);
		this.paramsCollection.add("receiptAddress_city_cmb",
				receiptAddress_city_cmb);
		this.paramsCollection.add("receiptAddress_grid", receiptAddress_grid);
		return receiptAddress_grid;

	},
	initEvent:function(){
		var module=this;
		var win=module.getParamsByKey("win");
		win.on('show',function(){
			this.getParamsByKey("receiptAddress_ds").load({
				 callback: function(records,options,success){
		         
		         if(success){
		        	 
		        	 //Ext.Msg.alert('提示',"加载数据成功");
		         }else{
		        	 
		        	 
		        	 Ext.Msg.alert('提示',"网络连接异常...请稍候再试");
		         }			  
			
		 }			
	
	});
		},module,{single:true});
	},
	/**
	 * Create this modules window here.
	 */
	createWindow : function() {
		var desktop = this.app.getDesktop();
		var win = desktop.getWindow('receiptAddress');
		var module=this;
		if (!win) {
			win = desktop.createWindow({
						id : 'receiptAddress',
						title : '收货地址列表',
						width : document.body.clientWidth * 0.9,
						height : document.body.clientHeight * 0.9,
						iconCls : 'acc-icon',
						shim : false,
						animCollapse : false,
						constrainHeader : true,
						maximizable : true,
						taskbuttonTooltip : "收货地址列表",
						layout : "fit",
						listeners : {
							'resize' : function(win, width, height) {
								if (width < 800 || height < 500) {
									win.setWidth(800);
									win.setHeight(500);
									this.items.get(0).setHeight(500);
								} else {
									this.items.get(0).setHeight(height);
								}
							}
						},
						tools : [{
									id : 'refresh',
									handler : Ext.emptyFn,
									scope : this
								}],
						items : [this.getComponents(module)]
					});
			module.paramsCollection.add("win",win);
			module.initEvent();


		}
		win.show();

	}
});